package com.turtle3d;

import java.util.ArrayList;
import java.util.Date;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3f;

import com.turtle3d.formallanguage.TurtleController;
import com.turtle3d.formallanguage.grammar.Grammar;
import com.turtle3d.formallanguage.symbol.Symbol;
import com.turtle3d.formallanguage.symbol.SymbolFactory;
import com.turtle3d.gui.PrintCanvas3D;
import com.turtle3d.gui.toolbox.GenerateFractalEvent;
import com.turtle3d.gui.toolbox.GenerateFractalListener;
import com.turtle3d.turtle.Turtle3D;
import com.turtle3d.turtle.Turtle3DImplementation;
import com.turtle3d.visualizator.Turtle3DVisualisator;

public class FractalGenerator implements GenerateFractalListener
{
	
	private PrintCanvas3D printCanvas3D;

	public void generate(GenerateFractalEvent generateFractalEvent)
	{		
		
		Turtle3D t = new Turtle3DImplementation();

		TurtleController tm = new TurtleController(t);

		Grammar g = Grammar.createTestGrammar2();

		Date start = new Date();
		SymbolFactory.getSymbolFactory().setDefaultParameter("iterationsCount",
				(float)generateFractalEvent.getIterations());
		ArrayList<Symbol> symbols = g.generateSymbolSequence2(generateFractalEvent.getIterations());
		// System.out.println(Grammar.printSymbolSequence(symbols, false));
		long symbolCount = symbols.size();
		Date symbolSequenceReady = new Date();

		tm.interpretLSymbols(symbols);
		Date turtleMovementReady = new Date();		

		BranchGroup branchGroup = Turtle3DVisualisator.createBranchGroup(tm
				.getVisualData());
		
		printCanvas3D.drawScene(branchGroup);
		
		//SymbolFactory.getSymbolFactory().initDefaultParameters();
		
		Date sceneReady = new Date();
		
		System.out.println("Symbol squence length: "+symbolCount);
        System.out.println("Time spent on:");
        System.out.println("Symbol sequence generation: "+(symbolSequenceReady.getTime()-start.getTime())/1000.+"s");
        System.out.println("Turtle movement: "+(turtleMovementReady.getTime()-symbolSequenceReady.getTime())/1000.+"s");
        System.out.println("Scene preperation: "+(sceneReady.getTime()-turtleMovementReady.getTime())/1000.+"s");
		
	}

	public PrintCanvas3D getPrintCanvas3D()
	{
		return printCanvas3D;
	}

	public void setPrintCanvas3D(PrintCanvas3D printCanvas3D)
	{
		this.printCanvas3D = printCanvas3D;
	}

}
